﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.Security;

public static class DBArticles
{
    private static string _connString =
        ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();

    public static DataTable GetArticlesForUser(string username="admin")
    {
        var adp = new SqlDataAdapter(string.Format(@"Select  A.[ArticleID],[ArticleName], [ArticlesText] from
        Articles A inner join UsersArticlesConnection UA 
        on A.ArticleID = UA.ArticleID
        where
        UA.UserID = '{0}'",username), _connString);
        DataSet ds = new DataSet("Articles");
        adp.Fill(ds, "Articles");
        return ds.Tables["Articles"];
    }

    public static DataTable GetArticleByID(string ID)
    {
        var adp = new SqlDataAdapter((string.Format(@"SELECT [ArticleName]
      ,[ArticlesText]
      FROM [SLTestProject].[dbo].[Articles]
      WHERE ArticleID = '{0}'",ID)),_connString);
      DataSet ds = new DataSet("Article");
        adp.Fill(ds, "Article");
        return ds.Tables["Article"];
    }

   /* public static void RemoveUser(string username)
    {
        Membership.DeleteUser(username);
    }

    public static bool AddUser(string username, string rolename, string password="123456")
    {
        if (Membership.GetUser(username) != null)
            throw new Exception("User is already exists");
        Membership.CreateUser(username, password);
        if (Membership.ValidateUser(username,password) && Roles.RoleExists(rolename))
        {
            Roles.AddUsersToRole(username.Split(),rolename);
        }
        return true;

    }

    public static void UpdateUser(string username, string rolename)
    {
        var user = Membership.GetUser(username);
        if(user==null)
            throw new Exception("Incorrect username");
        if (Roles.GetRolesForUser(user.UserName).Length>0)
            Roles.RemoveUserFromRoles(user.UserName, Roles.GetRolesForUser(user.UserName));
        if(!string.IsNullOrEmpty(rolename))
            Roles.AddUserToRole(username,rolename);
    }*/
}
